package cn.bincker.mycaptcha.slider;

import cn.bincker.mycaptcha.VerifyRequestAnswerResolver;
import cn.bincker.mycaptcha.slider.entity.SliderAnswer;
import cn.bincker.mycaptcha.basic.exception.CaptchaVerifyFailException;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 默认的滑块验证码验证请求的答案解析器
 */
public class SliderVerifyRequestAnswerResolver implements VerifyRequestAnswerResolver<SliderAnswer> {
    @Override
    public SliderAnswer resolver(HttpServletRequest request) {
        var x = request.getParameter("x");
        if(!StringUtils.hasText(x)) throw new CaptchaVerifyFailException();
        try {
            return new SliderAnswer(Integer.parseInt(x));
        }catch (NumberFormatException e){
            throw new CaptchaVerifyFailException("参数错误: x=" + x, e);
        }
    }
}
